package com.forum.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.forum.common.utils.R;
import com.forum.community.entity.ForumCommunity;
import com.forum.community.entity.vo.CommunityQuery;
import com.forum.community.service.ForumCommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bncgnc
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/community/forumCommunity")
public class ForumCommunityController {

    @Autowired
    private ForumCommunityService communityService;

    //社区添加,传入社区创建者的id，并且加入到forumuser表内
    @PostMapping("createCommunity")
    public R createCommunity(@RequestBody ForumCommunity forumCommunity){
        int i = communityService.createCommunity(forumCommunity);
        if(i>0){
            return R.ok("创建社区成功！").put("communityId",forumCommunity.getId());
        }
        return R.error("创建社区失败！");
    }

    //社区删除,删除user表的id
    @DeleteMapping("deleteCommunity/{communityId}")
    public R deleteCommunity(@PathVariable Long communityId){
        int i = communityService.deleteCommunity(communityId);
        if(i>0){
            return R.ok("删除社区成功！");
        }
        return R.error("删除社区失败！");
    }

    //社区分页条件查询
    @PostMapping("pageConditionCommunity/{current}/{limit}")
    public R pageConditionCommunity(@PathVariable Long current, @PathVariable Long limit, @RequestBody(required = false) CommunityQuery communityQuery){
        Page<ForumCommunity> page = new Page<>(current,limit);
        if(communityQuery!=null){
            QueryWrapper<ForumCommunity> wrapper = new QueryWrapper<>();
            String name = communityQuery.getCommunityTheme();
            String begin = communityQuery.getBegin();
            String end = communityQuery.getEnd();

            if(!StringUtils.isEmpty(name)){
                wrapper.like("community_theme",name);
            }

            if(!StringUtils.isEmpty(begin)){
                wrapper.ge("create_time",begin);
            }
            if(!StringUtils.isEmpty(end)){
                wrapper.le("create_time",end);
            }
            communityService.page(page,wrapper);
        }else{
            communityService.page(page,null);
        }

        Long total = page.getTotal();
        List<ForumCommunity> records = page.getRecords();
        return R.ok().put("rows",records).put("total", total);
    }

    //根据社区id查询该社区的信息
    @GetMapping("getCommunity/{communityId}")
    public R getVisited(@PathVariable Long communityId){
        ForumCommunity community = communityService.getById(communityId);
        if(community ==null){
            return R.error("社区不存在！");

        }
        return R.ok("查询社区成功！").put("forumCommunity",community);
    }

    //根据社区id修改社区信息
    @PostMapping("editCommunity")
    public R editCommunity(@RequestBody ForumCommunity forumCommunity){
        boolean i = communityService.updateById(forumCommunity);
        if(i){
            return R.ok("修改社区成功！");
        }
        return R.error("修改社区失败！");
    }

}

